package com.hzkq.widgets.chart.simple;

import android.graphics.Color;
import android.graphics.DashPathEffect;
import android.graphics.Paint;
import android.graphics.Paint.Style;
import android.os.Bundle;

import com.hzkq.widgets.R;
import com.keqiang.chart.impl.pie.IndicatorLine;
import com.keqiang.chart.impl.pie.LabelPosition;
import com.keqiang.chart.impl.pie.impl.normal.PieChart;
import com.keqiang.chart.impl.pie.impl.normal.PieChartHighLightRender;
import com.keqiang.chart.impl.pie.impl.normal.PieChartView;
import com.keqiang.chart.impl.pie.impl.normal.PieEntity;
import com.keqiang.chart.utils.UtilsKt;

import java.util.ArrayList;
import java.util.List;

import androidx.annotation.Nullable;
import kotlin.Pair;
import me.zhouzhuo810.magpiex.ui.act.BaseActivity;
import me.zhouzhuo810.magpiex.ui.widget.TitleBar;
import me.zhouzhuo810.magpiex.utils.SimpleUtil;

/**
 * @author Created by wanggaowan on 2022/7/26 15:00
 */
public class PieChartActivity extends BaseActivity {
    
    private TitleBar mTitleBar;
    private PieChart mPieChart;
    private PieChart mPieChart2;
    private PieChart mPieChart3;
    
    @Override
    public int getLayoutId() {
        return R.layout.activity_simple_pie_chart;
    }
    
    @Override
    public boolean shouldSupportMultiLanguage() {
        return false;
    }
    
    @Override
    public void initView(@Nullable Bundle savedInstanceState) {
        mTitleBar = findViewById(R.id.title_bar);
        PieChartView pieChartView = findViewById(R.id.pie_chart);
        mPieChart = pieChartView.getChart();
        PieChartView pieChartView2 = findViewById(R.id.pie_chart2);
        mPieChart2 = pieChartView2.getChart();
        PieChartView pieChartView3 = findViewById(R.id.pie_chart3);
        mPieChart3 = pieChartView3.getChart();
        
        initPieChart();
        initPieChart2();
        initPieChart3();
    }
    
    @Override
    public void initData() {
        fillPieChartData();
        fillPieChartData2();
        fillPieChartData3();
    }
    
    @Override
    public void initEvent() {
        mTitleBar.getLlLeft().setOnClickListener(v -> closeAct());
    }
    
    private void initPieChart() {
        mPieChart.getCustomRender();
        mPieChart.getTouchHandler();
        mPieChart.getHighLightRender();
        mPieChart.getEmptyDataRender();
        
        mPieChart.setRadius(SimpleUtil.getScaledValue(300));
        mPieChart.setLabelPosition(LabelPosition.inside());
        
        PieChartHighLightRender render = new PieChartHighLightRender(mPieChart);
        render.setOffsetRadius(0f);
        render.setHighLightColor(pieEntity -> UtilsKt.setColorLuminance(pieEntity.getColor(), 0.7f));
        mPieChart.setHighLightRender(render);
        
        Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
        paint.setPathEffect(new DashPathEffect(new float[]{10f, 10f}, 0));
        paint.setStyle(Style.STROKE);
        paint.setStrokeWidth(SimpleUtil.getScaledValue(6));
        mPieChart.setCustomRender((canvas, start, top, end, bottom) -> {
            Pair<Float, Float> center = mPieChart.getCenter();
            List<? extends PieEntity> data = mPieChart.getData();
            float maxRadius = 0f;
            if (data != null) {
                for (PieEntity entity : data) {
                    if (maxRadius < entity.getDrawRadius()) {
                        maxRadius = entity.getDrawRadius();
                    }
                }
            }
            
            maxRadius += SimpleUtil.getScaledValue(10);
            canvas.drawCircle(center.component1(), center.component2(), maxRadius, paint);
        });
    }
    
    private void initPieChart2() {
        mPieChart2.setRadius(SimpleUtil.getScaledValue(300));
        mPieChart2.setInnerRadius(SimpleUtil.getScaledValue(200));
        mPieChart2.setSliceSpace(SimpleUtil.getScaledValue(10));
        mPieChart2.setLabelPosition(LabelPosition.outside()
            .lineFactory(entity -> IndicatorLine.newInstance()
                .color(entity.getColor())
                .offset(SimpleUtil.getScaledValue(6f))
                .minLength(SimpleUtil.getScaledValue(100))
                .drawStartCircle(true)
                .circleColor(entity.getColor())));
    }
    
    private void initPieChart3() {
        mPieChart3.setRadius(SimpleUtil.getScaledValue(240));
        mPieChart3.setInnerRadius(SimpleUtil.getScaledValue(200));
        mPieChart3.setSliceSpace(SimpleUtil.getScaledValue(1));
        
        PieChartHighLightRender render = new PieChartHighLightRender(mPieChart3);
        render.setOffsetRadius(0f);
        render.setAddedRadius(SimpleUtil.getScaledValue(30f));
        mPieChart3.setHighLightRender(render);
    }
    
    /**
     * 设置图表数据
     */
    private void fillPieChartData() {
        List<PieEntity> data = new ArrayList<>();
        for (int j = 0; j < 4; j++) {
            PieEntity entity = new PieEntity((float) (Math.random() * 200f));
            entity.setDrawLabel(true);
            entity.setLabel(((int) entity.getValue()) + "");
            entity.setLabelSize(SimpleUtil.getScaledValue(42));
            entity.setSecondLabelSize(SimpleUtil.getScaledValue(30));
            entity.setLabelColor(Color.WHITE);
            entity.setSecondLabelColor(Color.GRAY);
            
            if (j % 4 == 0) {
                entity.setColor(Color.GREEN);
                entity.setSecondLabel("手游");
            } else if (j % 4 == 1) {
                entity.setColor(Color.DKGRAY);
                entity.setSecondLabel("主机");
            } else if (j % 4 == 2) {
                entity.setColor(Color.RED);
                entity.setSecondLabel("PC");
            } else {
                entity.setColor(Color.BLUE);
                entity.setSecondLabel("其它");
            }
            data.add(entity);
        }
        mPieChart.setData(data);
        mPieChart.animDraw(2000);
    }
    
    /**
     * 设置图表数据
     */
    private void fillPieChartData2() {
        List<PieEntity> data = new ArrayList<>();
        for (int j = 0; j < 4; j++) {
            PieEntity entity = new PieEntity((float) (Math.random() * 200f));
            entity.setDrawLabel(true);
            entity.setLabel(((int) entity.getValue()) + "");
            entity.setLabelSize(SimpleUtil.getScaledValue(42));
            entity.setSecondLabelSize(SimpleUtil.getScaledValue(30));
            if (j % 4 == 0) {
                entity.setColor(Color.GREEN);
                entity.setLabelColor(Color.GREEN);
                entity.setSecondLabel("手游");
            } else if (j % 4 == 1) {
                entity.setColor(Color.DKGRAY);
                entity.setLabelColor(Color.DKGRAY);
                entity.setSecondLabel("主机");
            } else if (j % 4 == 2) {
                entity.setColor(Color.RED);
                entity.setLabelColor(Color.RED);
                entity.setSecondLabel("PC");
            } else {
                entity.setColor(Color.BLUE);
                entity.setLabelColor(Color.BLUE);
                entity.setSecondLabel("其它");
            }
            data.add(entity);
        }
        mPieChart2.setData(data);
        mPieChart2.animDraw(2000);
    }
    
    /**
     * 设置图表数据
     */
    private void fillPieChartData3() {
        List<PieEntity> data = new ArrayList<>();
        for (int j = 0; j < 4; j++) {
            PieEntity entity;
            if (j % 4 == 0) {
                entity = new PieEntity(25);
                entity.setColor(Color.GREEN);
            } else if (j % 4 == 1) {
                entity = new PieEntity(25);
                entity.setColor(Color.DKGRAY);
                entity.setAddedRadius(SimpleUtil.getScaledValue(50));
            } else if (j % 4 == 2) {
                entity = new PieEntity(25);
                entity.setColor(Color.RED);
                entity.setAddedRadius(SimpleUtil.getScaledValue(100));
            } else {
                entity = new PieEntity(25);
                entity.setColor(Color.BLUE);
                entity.setAddedRadius(SimpleUtil.getScaledValue(150));
            }
            data.add(entity);
        }
        mPieChart3.setDataNotify(data);
    }
}
